package com.cnhis.cloudhealth.clinical.clidoctor.clisendadvice.bo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.cnhis.cloudhealth.clinical.clidoctor.basicinformationquery.bo.BasicInformationQueryBo;
import com.cnhis.cloudhealth.clinical.clidoctor.clibackadvice.bo.CliBackAdviceBo;
import com.cnhis.cloudhealth.clinical.clidoctor.clinewadvice.bo.CliNewAdviceBo;
import com.cnhis.cloudhealth.clinical.clidoctor.clinewadvice.dao.CliNewAdviceDao;
import com.cnhis.cloudhealth.clinical.clidoctor.clinicaldoctoradviceeditor.bo.DoctorAdviceEditorBo;
import com.cnhis.cloudhealth.clinical.clidoctor.clisendadvice.dao.CliSendAdviceDao;
import com.cnhis.cloudhealth.clinical.clidoctor.patientsearch.bo.PatientSearchBo;
import com.cnhis.cloudhealth.clinical.clidoctor.removeAdvice.bo.RemoveAdviceBo;
import com.cnhis.cloudhealth.clinical.util.ExUtil;
import com.cnhis.cloudhealth.clinical.util.PiplineUtil;
import com.cnhis.cloudhealth.clinical.util.RunEx;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.commonSql.bo.YaopinKucunBo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.util.oplogutil.AsynUtil;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.google.common.collect.Maps;

import net.sf.json.JSONObject;

@SuppressWarnings("rawtypes")
@Service
// only half perf  ,caus conflict with mapen
public class CliSendAdviceBo_s412
{
    @Resource
    private CliNewAdviceDao cliNewAdviceDao;
    
    @Resource
    private CliSendAdviceDao cliSendAdviceDao;
    
    @Resource
    private CliBackAdviceBo cliBackAdviceBo;
    
    @Resource
    private RemoveAdviceBo removeAdviceBo;
    
    @Resource
    private YaopinKucunBo yaopinKucunBo;
    
    @Resource
    private PatientSearchBo patientSearchBo;
    
    @Resource
    private CliNewAdviceBo cliNewAdviceBo;
    
    @Resource
    private GetNewId getNewId;
    
    @Resource
    private BasicInformationQueryBo basicInformationQueryBo;
    
    @Resource
    private DoctorAdviceEditorBo doctorAdviceEditorBo;
    
    Logger log = Logger.getLogger(CliSendAdviceBo.class);
    
    /**
     * 直接发送医嘱（保存-发送）
     * @param map
     * @return
     * @throws Exception
     */
    public ModelVo execClinicAdviceSendPretreat(Map map)
        throws Exception
    {
        long t1 = System.currentTimeMillis();
    	//参数类型转换
    	map.put("lBCE01", GlobalVariable.IntegerIsNull(map.get("lBCE01")));
    	map.put("lbce01", GlobalVariable.IntegerIsNull(map.get("lBCE01")));
    	map.put("lBCK01", GlobalVariable.IntegerIsNull(map.get("lBCK01")));
    	map.put("lbck01", GlobalVariable.IntegerIsNull(map.get("lBCK01")));
    	map.put("lSign", GlobalVariable.IntegerIsNull(map.get("lSign")));
    	map.put("lsign", GlobalVariable.IntegerIsNull(map.get("lSign")));
    	map.put("lVAA07", GlobalVariable.LongIsNull(map.get("lVAA07")));
    	map.put("lVAA01", GlobalVariable.LongIsNull(map.get("lVAA01")));
    	map.put("lbce02", GlobalVariable.StringIsNull(map.get("lBCE02")));
    	map.put("lbce03", GlobalVariable.StringIsNull(map.get("lBCE03")));
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
        //保存医嘱
        ModelVo vafMv = cliNewAdviceBo.insertClinicVAF1(map);
        //获取费用
        List<Map<String, Object>> vafList = (List<Map<String, Object>>)vafMv.getList();
        Set<Long> cbm01Set = new HashSet<Long>();
        List<Map<String, Object>> cbm01SqdList = new ArrayList<Map<String, Object>>();//申请单用
        List<Map<String, Object>> vaf01sLsit = new ArrayList<Map<String, Object>>();
        for(Map<String, Object> temp : vafList)
        {
            String bda01 = GlobalVariable.StringIsNull(temp.get("bda01"));
            long vaf01a = GlobalVariable.LongIsNull(temp.get("vaf01a"));
            int vaf32 = GlobalVariable.IntegerIsNull(temp.get("vaf32"));
            cbm01Set.add(GlobalVariable.LongIsNull(temp.get("cbm01"), 0L));
            if(judgeFlag(bda01,vaf01a,vaf32))
            {
                Map<String, Object> vaf01Map = new HashMap<String, Object>();
                vaf01Map.put("vaf01", GlobalVariable.LongIsNull(temp.get("vaf01")));
                vaf01sLsit.add(vaf01Map);
            }
        }
        String cbm01s = "";
        for(Long l : cbm01Set)
        {
            cbm01s += ","+l;
           inner: for(Map<String, Object> temp : vafList)
            {
                if ((""+l).equals(GlobalVariable.StringIsNull(temp.get("cbm01"))) && "LE".indexOf(GlobalVariable.StringIsNull(temp.get("bda01"))) >= 0)
                {
                    cbm01SqdList.add(temp);
                    break inner;
                }
            }
        }
        if (cbm01s.length()>1)
        {
            cbm01s = cbm01s.substring(1);
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("sqlId", 2118);
        paramMap.put("cbm01", "any('{"+cbm01s+"}'::int8[])");
         List<Map<String, Object>> VBD1List = (List<Map<String, Object>>)basicInformationQueryBo.queryBase(paramMap).getList();
//        double totalVbi = 0;
        for(Map<String, Object> temp : VBD1List)
        {
            temp.put("vaf01", temp.get("vaf01"));
            temp.put("bby01", temp.get("bby01"));
            temp.put("vbd04", temp.get("vaf21"));// 数量
            temp.put("bby25", temp.get("bby25"));// 剂量单价
//            totalVbi += parseFloat($("#bencidaijie").text());//遍历累加消费金额
            temp.put("vbd05", temp.get("lprice"));// 门诊包装单价
            temp.put("vbd06", temp.get("bdu06"));// 是否有从项
            temp.put("bck01", temp.get("bck01"));// 执行科室
            temp.put("zxks", temp.get("bck01"));// 执行科室
            temp.put("bdn01", temp.get("bdn01"));// 医嘱项目类型
            temp.put("vbd08", temp.get("bdu07"));// 费用性质
            temp.put("vbd09", temp.get("bdu10"));// 收费方式
            temp.put("dsk01",  temp.get("dsk01"));// 药品批次
            temp.put("bby01a", temp.get("bby01a"));// 收费套餐ID
            temp.put("vbd12", temp.get("vbd12"));// 诊疗收费对照数量
            temp.put("bag05", temp.get("bag05"));// 诊疗收费对照数量
            temp.put("bby05", temp.get("bby05"));// 诊疗收费对照数量
        }
        //库存校验
//        List<Map<String, Object>> bby01Arr_1 = new ArrayList<Map<String, Object>>();
//        List<Map<String, Object>> bby01Arr_11 = new ArrayList<Map<String, Object>>();// 储存西、成药品需要数量和库存数量的json数组
//        List<Map<String, Object>> bby01Arr_4 = new ArrayList<Map<String, Object>>();
//        List<Map<String, Object>> bby01Arr_41 = new ArrayList<Map<String, Object>>();// 储存材料需要数量和库存数量的json数组
//        for (int i = 0; i < VBD1List.size(); i++)
//        {
//            Map<String, Object> temp = VBD1List.get(i);
//            if(VBD1List.get(i).get("bck01")==null)
//            {
//                mo.setResult(StaticKeys.OH_FALSE);
//                mo.setResultMsg(VBD1List.get(i).get("bby05")+"执行科室配置错误");
//                return mo;
//            }
//            if("123".indexOf(GlobalVariable.StringIsNull(temp.get("bdn01")))>=0)
//            {
//                bby01Arr_11.add(temp);
//                bby01Arr_1.add(temp);
//            }
//            else if("4M".indexOf(GlobalVariable.StringIsNull(temp.get("bdn01")))>=0)
//            {
//                bby01Arr_41.add(temp);
//                bby01Arr_4.add(temp);
//            }
//        }
//
//        HashMap<String, Object> cailiaoMap = new HashMap<String, Object>();
//        String p_100_105002_404 = getParameter1(GlobalVariable.LongIsNull(map.get("lbck01")),100105002404L);
//        cailiaoMap.put("bck01_c", p_100_105002_404=="-1"?map.get("lbck01"):p_100_105002_404);
//        cailiaoMap.put("lACF01", 1);
//        cailiaoMap.put("bby01Arr_4", bby01Arr_4);
//        cailiaoMap.put("productid", 9999);
//        cailiaoMap.put("programid", 9999);
//        cailiaoMap.put("paramno", 15);
//        String msg = "";
//        List<Map<String, Object>> cailiaoList = (List<Map<String, Object>>)doctorAdviceEditorBo.checkdrugstoreNew_cailiao(cailiaoMap).getList();
//        for (int i = 0; i < cailiaoList.size(); i++)
//        {
//            for (int j = 0; j < bby01Arr_41.size(); j++)
//            {
//                if (bby01Arr_41.get(j).get("bby01") .equals(cailiaoList.get(i).get("bby01")) )
//                {
//                    bby01Arr_41.get(j).put("amount", cailiaoList.get(i).get("amount"));
//                }
//            }
//        }
//        for (int i = 0; i < bby01Arr_41.size(); i++)
//        {
//            if (bby01Arr_41.get(i).get("amount")==null)
//            {
//                msg += bby01Arr_4.get(i).get("bby05") + "--需要数量为" + bby01Arr_41.get(i).get("vaf21") + ",当前库存数量为0;<br/>";
//            }
//            else if ((GlobalVariable.DoubleIsNull(bby01Arr_41.get(i).get("amount")) < GlobalVariable.DoubleIsNull(bby01Arr_41.get(i).get("vaf21"))))
//            {
//                msg += bby01Arr_41.get(i).get("bby05") + "--需要数量为" + bby01Arr_41.get(i).get("vaf21") + ",当前库存数量为" + bby01Arr_41.get(i).get("amount") + ";<br/>";
//            }
//        }
//        if ("" != msg)
//        {
//            mo.setResult(StaticKeys.OH_FALSE);
//            mo.setResultMsg(msg);
//            return mo;
//        }
//
//     // 确认医嘱前检验药品库存是否足够
//        HashMap<String, Object> yaopinMap = new HashMap<String, Object>();
//        String p_100_105002_108 = getParameter1(GlobalVariable.LongIsNull(map.get("lbck01")),100105002108L);
//        String p_100_105002_110 = getParameter1(GlobalVariable.LongIsNull(map.get("lbck01")),100105002110L);
//        yaopinMap.put("bck01_x", p_100_105002_108);
//        yaopinMap.put("bck01_z", p_100_105002_110);
//        yaopinMap.put("lACF01", 1);
//        yaopinMap.put("bby01Arr_1", bby01Arr_1);
//        yaopinMap.put("productid", 9999);
//        yaopinMap.put("programid", 9999);
//        yaopinMap.put("paramno", 15);
//        List<Map<String, Object>> yaopinList = (List<Map<String, Object>>)doctorAdviceEditorBo.checkdrugstoreNew(yaopinMap).getList();
//        for (int i = 0; i < yaopinList.size(); i++)
//        {
//            for (int j = 0; j < bby01Arr_11.size(); j++)
//            {
//                if (bby01Arr_11.get(j).get("bby01") .equals(yaopinList.get(i).get("bby01")) )
//                {
//                    bby01Arr_11.get(j).put("amount",yaopinList.get(i).get("amount"));
//                }
//            }
//        }
//        for (int i = 0; i < bby01Arr_11.size(); i++)
//        {
//            if (bby01Arr_11.get(i).get("amount")==null)
//            {
//                msg += bby01Arr_1.get(i).get("bby05") + "--需要数量为" + bby01Arr_11.get(i).get("vaf21") + ",当前库存数量为0;<br/>";
//            }
//            else if ((GlobalVariable.DoubleIsNull(bby01Arr_11.get(i).get("amount")) < GlobalVariable.DoubleIsNull(bby01Arr_11.get(i).get("vaf21"))))
//            {
//                msg += bby01Arr_11.get(i).get("bby05") + "--需要数量为" + bby01Arr_11.get(i).get("vaf21") + ",当前库存数量为" + bby01Arr_11.get(i).get("amount") + ";<br/>";
//            }
//        }
//        if ("" != msg)
//        {
//            mo.setResult(StaticKeys.OH_FALSE);
//            mo.setResultMsg(msg);
//            return mo;
//        }
        //发送医嘱
        Map<String,Object> xmlMap = new HashMap<String,Object>();
        Map<String,Object> sendMap = new HashMap<String,Object>();
        xmlMap.put("VBD1", VBD1List);
        xmlMap.put("VAF1", JSON.toJSONString(vaf01sLsit));
        xmlMap.put("zjAddList", JSON.toJSONString(map.get("zjAddList")));
        sendMap.put("lXml", JSON.toJSONString(xmlMap));
        sendMap.put("lBCE01", GlobalVariable.IntegerIsNull(map.get("lBCE01")));
        sendMap.put("lbce01", GlobalVariable.IntegerIsNull(map.get("lBCE01")));
        sendMap.put("lBCK01", GlobalVariable.IntegerIsNull(map.get("lBCK01")));
        sendMap.put("lbck01", GlobalVariable.IntegerIsNull(map.get("lBCK01")));
        sendMap.put("lSign", GlobalVariable.IntegerIsNull(map.get("lSign")));
        sendMap.put("lsign", GlobalVariable.IntegerIsNull(map.get("lSign")));
        sendMap.put("lVAA07", GlobalVariable.LongIsNull(map.get("lVAA07")));
        sendMap.put("lVAA01", GlobalVariable.LongIsNull(map.get("lVAA01")));
        sendMap.put("lbce02", GlobalVariable.StringIsNull(map.get("lBCE02")));
        sendMap.put("lbce03", GlobalVariable.StringIsNull(map.get("lBCE03")));
        sendMap.put("savePrintList", JSON.toJSONString(map.get("savePrintList")));
        ModelVo moSend = execClinicAdviceSend(sendMap);
        moSend.setData(cbm01SqdList);
        long t2 = System.currentTimeMillis();
        log.info("=======================保存、发送医嘱总耗时==================="+(t2-t1)+"毫秒======================");
        return moSend;
    }
    
    private boolean judgeFlag(String bda01, long vaf01a, int vaf32)
    {
        boolean flag = false;
        if ("128".indexOf(bda01) >= 0)
        {
            flag = true;
        }
        if ("SE".indexOf(bda01) >= 0 && vaf01a == 0)
        {
            flag = true;
        }
        if ("1238SE".indexOf(bda01) < 0 && vaf01a == 0 && vaf32 == 0)
        {
            flag = true;
        }
        return flag;
    }
    
    private Map getOperation(Map map)
        throws Exception
    {
        return cliSendAdviceDao.getlBCE02AndlBCE03(map);
    }
    
    
    @SuppressWarnings("unchecked")
    public String getParameter(Integer productId, Integer programId, Integer paramNo)
        throws Exception
    {
        HashMap map = new HashMap();
        map.put("productId", productId);
        map.put("programId", programId);
        map.put("paramNo", paramNo);
        return cliSendAdviceDao.getParameter(map);
    }
    
    @SuppressWarnings("unchecked")
    public String getParameter1(Long userid,Long paramid)
        throws Exception
    {
        HashMap map = new HashMap();
        map.put("userid", userid);
        map.put("paramid", paramid);
        return cliSendAdviceDao.getParameter1(map);
    }
    public ModelVo getclinicEditOrderBeforeJudge(Map map)
        throws Exception
    {
        
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
        
        // 如果是非门诊，则返回成功
        if (!"1".equals(map.get("ACF01").toString()))
        {
            return mo;
        }
        // 是否有限制条件
        if (getBlp02(map))
        {
            return mo;
        }
        
        // 获取限定年龄
        Integer BLP04 = getBlp04(map);
        
        // 获取病人年龄
        Integer VAA10 = getVaa10(map);
        
        if ((VAA10 >= BLP04) && getVcj04Vcj05(map))
        {
            mo.setResult(StaticKeys.OH_FALSE);
            mo.setResultMsg("病人超过" + BLP04 + "岁，必须要输入收缩压和舒张压.");
            return mo;
        }
        
        return mo;
    }
    
    /**
     * 入收缩压和舒张压 是否为空
     * 
     * @param map
     * @return
     * @throws Exception
     */
    private boolean getVcj04Vcj05(Map map)
        throws Exception
    {
        int ct = cliNewAdviceDao.getVcj04Vcj05(map);
        if (ct > 0)
        {
            return true;
        }
        return false;
    }
    
    /**
     * 获取病人年龄
     * 
     * @param map
     * @return
     * @throws Exception
     */
    private Integer getVaa10(Map map)
        throws Exception
    {
        Integer VAA10 = cliNewAdviceDao.getVaa10(map);
        VAA10 = VAA10 == null ? 0 : VAA10;
        return VAA10;
    }
    
    /**
     * 获取限定年龄
     * 
     * @param map
     * @return
     * @throws Exception
     */
    private Integer getBlp04(Map map)
        throws Exception
    {
        Integer BLP04 = cliNewAdviceDao.getBlp04(map);
        BLP04 = BLP04 == null ? 0 : BLP04;
        return BLP04;
    }
    
    /**
     * 是否有限制条件
     * 
     * @param map
     * @return
     * @throws Exception
     */
    private boolean getBlp02(Map map)
        throws Exception
    {
        int ct = cliNewAdviceDao.getBlp02(map);
        if (ct == 0)
        {
            return true;
        }
        return false;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo querySendAdvice(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List listAll = new ArrayList();
        List list = null;
        try
        {
            if ("0".equals(map.get("lDay").toString()))
            {
                map.put("aDate", "1899-12-30");
            }
            else
            {
                String aDate = aDate(map);
                map.put("aDate", aDate);
            }
            list = cliSendAdviceDao.querySendAdvice1(map);
            listAll.addAll(list);
            list = cliSendAdviceDao.querySendAdvice2(map);
            listAll.addAll(list);
            list = cliSendAdviceDao.querySendAdvice3(map);
            listAll.addAll(list);
            list = cliSendAdviceDao.querySendAdvice4(map);
            listAll.addAll(list);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(list);
        return mo;
    }
    
    private String aDate(Map map)
        throws Exception
    {
        return cliSendAdviceDao.aDate(map);
    }
    
    public ModelVo checkWhetherDurg(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = cliSendAdviceDao.checkWhetherDurg(map);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(list);
        return mo;
    }
    
    public ModelVo checkWhetherVIP(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = cliSendAdviceDao.checkWhetherVIP(map);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(list);
        return mo;
    }
    
    public ModelVo getFeeItemExecutionOffices(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = cliSendAdviceDao.getFeeItemExecutionOffices(map);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(list);
        return mo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo cardBalance(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            map.put("Rvalue", 0);
            Map m = cliSendAdviceDao.cardBalance(map);
            if (m != null && m.size() > 0)
            {
                BigDecimal VBU14 = BigDecimal.valueOf(Double.parseDouble(m.get("VBU14").toString()));
                BigDecimal VBU15 = BigDecimal.valueOf(Double.parseDouble(m.get("VBU15").toString()));
                BigDecimal BEP06B = BigDecimal.valueOf(Double.parseDouble(m.get("BEP06B").toString()));
                BigDecimal VAJ38 = BigDecimal.valueOf(Double.parseDouble(map.get("VAJ38").toString()));
                map.put("VBU14", VBU14);
                map.put("VBU15", VBU15);
                if (VBU14.add(BEP06B).doubleValue() < VAJ38.doubleValue())
                {
                    map.put("Rvalue", 3);
                }
                
            }
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setMap(map);
        return mo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo execClinicAdviceSend(Map map)
        throws Exception
    {
        long l1 = System.currentTimeMillis();
        log.info("======================================================---发送医嘱开始==============================================--");
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
        try
        {
            
        String xml = map.get("lXml").toString();
        map.put("ReVAIid", "");
        map.put("newID", getNewId.nextId());
        JSONObject xmlJson = JSONObject.fromObject(xml);
        // 医嘱信息
        List VAF1List = JsonUtil.getList(xmlJson.getString("VAF1"));
        //判断医嘱是否已经发送
        if(map.get("savePrintList")!=null)
        {
            List savePrintList = JsonUtil.getList(map.get("savePrintList").toString());
            if(savePrintList!=null && !savePrintList.isEmpty())
            {
                Map tempMap = new HashMap<>();
                tempMap.put("vaf01", ((Map<String, Object>)savePrintList.get(0)).get("vaf01"));
                List vaf10TypeList = cliSendAdviceDao.queryForList("CliSendAdvice.vaf10TypeList", tempMap);
                if(vaf10TypeList!=null && !vaf10TypeList.isEmpty() && ("8".equals(((Map<String, Object>)vaf10TypeList.get(0)).get("vaf10")) || 8==Integer.parseInt((((Map<String, Object>)vaf10TypeList.get(0)).get("vaf10").toString()))))
                {
                    //回退、删除
                    String cbm01s = "";
                    String vaf01s = "";
                    
                    for(int i = 0;i<savePrintList.size();i++)
                    {
                        Map<String, Object> temp0 = (Map<String, Object>)savePrintList.get(i);
                        cbm01s += ","+temp0.get("cbm01");
                        vaf01s += ","+temp0.get("vaf01");
                    }
                    cbm01s = "{"+cbm01s.substring(1)+"}";
                    vaf01s = vaf01s.substring(1);
                    map.put("cbm01", cbm01s);
                    map.put("bce03", map.get("lBCE03"));
                    map.put("lbce01", map.get("lBCE01"));
                    map.put("lmember", "2");
                    map.put("vaf01s", vaf01s);
                    removeAdviceBo.backAdvice(map);
                    removeAdviceBo.removeAdvice(map);
                }
            }
        }
        // 医嘱计价信息
        List VBD1List = JsonUtil.getList(xmlJson.getString("VBD1"));
        // bdn01 = (String)((Map<String,Object>)VBD1List.get(0)).get("bdn01");
        // 门诊记账
        List VAJ1List = null;
        List tmpVAIList = null;
        List tmpVAJList = null;// TmpVAJ
        if (xmlJson.containsKey("VAJ1"))
        {
            String VAJ1 = xmlJson.getString("VAJ1");
            if (VAJ1 != null)
            {
                VAJ1List = JsonUtil.getList(VAJ1);
            }
        }
        // 获取员工编号和姓名
        Map uMap = getOperation(map);
        map.put("lBCE02", uMap.get("bce02"));
        map.put("lBCE03", uMap.get("bce03"));
        if (VAF1List == null || VAF1List.size() == 0)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            mo.setResultMsg("医嘱信息为空");
            return mo;
        }
        map.put("VAF1List", VAF1List);// TmpVafs
        map.put("VBD1List", VBD1List);// TmpVBD
        map.put("VAJ1List", VAJ1List);// tmpZj
        List tmpVafs = getTmpVafs(map);
        if (tmpVafs == null || tmpVafs.size() == 0)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            mo.setResultMsg("有医嘱已经发送或作废，请检查！");
            return mo;
        }
        map.put("tmpVafsList", tmpVafs);
        
        String param375 = getParameter(100, 105002, 375);// 门诊发送医嘱启用按收费方式计费
        if ("1".equals(param375))
        {
            String begdate = DateUtils.getDateString(new Date());
            map.put("begdate", begdate);
            VBD1List = getTmpVBD(map);
            map.put("VBD1List", VBD1List);
        }
        if (VBD09(map))
        {
            VBD1List = tempVBD1Param(map);
            map.put("VBD1List", VBD1List);
        }
        
        String lMember = getParameter(9999, 9999, 7);
        String lOne = getParameter(9999, 9999, 12);//门诊发送医嘱分单规则
        map.put("lOne", lOne);
        String Para244 = getParameter(100, 105002, 244);// 只判断当天记录，没有对应收费记录就自动增加诊金单据
        String Para258 = getParameter(100, 105002, 258);// 诊金默认单据选项
        String Para383 = getParameter(100, 105002, 383);// 发送医嘱一卡通扣所有科室费用
        String aStr = "0000000000";
        map.put("aStr", aStr);
        String aDate = DateUtils.getDateString(new Date());
        map.put("aDate", aDate);
        String EndDate = DateUtils.getDateStr(new Date()) + " 23:59:59";
        map.put("EndDate", EndDate);
        map.put("Para244", Para244);
        // 药品有效期禁用
    FutureTask ftsk_validity_pipline=    validity_pipline(map, mo);   //chgpart4
        map.put("ABC01", 0);
        Map m = getBCK01A(map);
        map.put("lABC02", m == null || m.get("labc02") == null ? "" : m.get("labc02").toString());
        map.put("lBCK01B", m == null || m.get("lbck01b") == null ? 0 : m.get("lbck01b").toString());
        if (tmpZJExistsParam(map))
        {
            tmpVAJList = tempVBD1Param(map);
            map.put("tmpVAJList", tmpVAJList);
            if (cbm01Count(map))
            {
                tmpVAIList = tmpVAIParam(map);
                map.put("tmpVAIList", tmpVAIList);
            }
        }
        tmpVAJList = tmpVAJParam(map);// 初始化
        long l2 = System.currentTimeMillis();
        log.info("----------------------------------------------数据、参数准备耗时:"+((l2-l1))+"毫秒------------------------------------------------------");
        map.put("tmpVAJList",tmpVAJList);
        if (tmpVAJList != null && tmpVAJList.size() > 0)
        {
            if ("1".equals(lOne) || "2".equals(lOne) || "3".equals(lOne))
            {
                map.put("tmpVAJList", tmpVAJList);
                tmpVAIList = tmpVAIpara(map);
                map.put("tmpVAIList", tmpVAIList);
                Integer maxID = getMaxID(map);
                Long aVAIid = getNewId.nextId() - maxID;
                map.put("aVAIid", aVAIid);
                tmpVAIList = setVAIID(map);
                map.put("tmpVAIList", tmpVAIList);
                map.put("tmpVAJList", tmpVAJList);
                tmpVAJList = tmpVAJParam1(map);
                map.put("tmpVAJList", tmpVAJList);
            }
            else
            {
                tmpVAIList = tmpVAIParam2(map);
                map.put("tmpVAIList", tmpVAIList);
                Integer maxID = getMaxID(map);
                Long aVAIid = getNewId.nextId() - maxID;
                map.put("aVAIid", aVAIid);
                tmpVAIList = setVAIID(map);
                for(Map<String, Object> temp : (List<Map<String, Object>>)tmpVAIList)
                {
                    temp.put("vai01", getNewId.nextId());
                }
                map.put("tmpVAIList", tmpVAIList);
                tmpVAJList = tmpVAJParam2(map);
                map.put("tmpVAJList", tmpVAJList);
            }
            if ("1".equals(Para258))
            {
                Integer aVAIid = MinVAI01(map);
                map.put("aVAIid", aVAIid);
                tmpVAIList = para258tmpVAI(map);
                tmpVAJList = para258tmpVAJ(map);
                map.put("tmpVAIList", tmpVAIList);
                map.put("tmpVAJList", tmpVAJList);
                
            }
            if ("2".equals(Para258))
            {
                if (Para258_2(map))
                {
                    Integer aVAIid = Para258_2_min(map);
                    map.put("aVAIid", aVAIid);
                    tmpVAIList = para258tmpVAI(map);
                    tmpVAJList = para258tmpVAJ(map);
                    map.put("tmpVAIList", tmpVAIList);
                    map.put("tmpVAJList", tmpVAJList);
                }
                else
                {
                    tmpVAIList = para258tmpVAI(map);
                    map.put("tmpVAIList", tmpVAIList);
                    tmpVAJList = Para258_2_tmpVAJ(map);
                    map.put("tmpVAJList", tmpVAJList);
                }
            }
            Integer maxId = getTmpVaj1MaxID(map);
            if (maxId == null)
            {
                maxId = 0;
            }
            map.put("aVAIid", getNewId.nextId() - maxId);
            map.put("tmpVafsList", tempVafsParam(map));
            map.put("tmpVAIList", tempVAI1(map));
            
            // fvaj25计算
            map.put("tmpVAJList", tempVaj1Param(map));
            // 药品如果禁售，就不允许在发药
            String bby05;
            FutureTask ftsk_yaopinJinshouChk= 	yaopinJinshouChk_pipline(map, mo);
            // 判断药品库存
            if (checkStock(map) && "2".equals(getParameter(9999, 9999, 21)))
            {
                bby05 = getBBY05(map);
                mo.setResult(StaticKeys.OH_FALSE);
                mo.setResultMsg(bby05 + "库存不够，不能发送医嘱");
                return mo;
            }
            
            
         
          
            
            
            if (("1".equals(lOne) || "2".equals(lOne) || "3".equals(lOne))
                && ("1".equals(lMember) || "2".equals(lMember)))
            {
                Map maps = getMinAndMaxVAI01(map);
                if (maps != null)
                {
                    Object avaiid = maps.get("avaiid");
                    Object avaimax = maps.get("avaimax");
                    if (avaiid != null && avaimax != null)
                    {
                        Long aVAIid = Long.parseLong(avaiid.toString());
                        Long aVAIMax = Long.parseLong(avaimax.toString());
                        for (; aVAIid < aVAIMax; aVAIid++)
                        {
                            map.put("aVAIid", aVAIid);
                            
                            if ((existsVAI1(map) && !"1".equals(Para383)) || "1".equals(Para383))
                            {
                                if ("".equals(map.get("ReVAIid").toString()))
                                {
                                    map.put("ReVAIid", aVAIid);
                                }
                                else
                                {
                                    map.put("ReVAIid", map.get("ReVAIid").toString() + "," + aVAIid);
                                }
                            }
                        }
                    }
                }
                
            }
            
            
            try {
            	ftsk_yaopinJinshouChk.get();
    		} catch (ExecutionException e) {
    			 		 
    			if(PiplineUtil.isEx("yaopinJinshouChk_err",e))
    			{
    				 
    				 return  (ModelVo) PiplineUtil.getRetObj(e);
    				
    			}else
    			ExUtil.throwExV2(e);
    		}
            
        }  //end   if (tmpVAJList !
        long l3 = System.currentTimeMillis();
        log.info("----------------------------------------------费用参数准备耗时:"+((l3-l2))+"毫秒------------------------------------------------------");
        
       
        
        
        try {
        	  ftsk_validity_pipline.get();
		} catch (ExecutionException e) {
			if(PiplineUtil.isEx("validity_check_false",e))
				return (ModelVo) PiplineUtil.getRetObj(e);
			else
			ExUtil.throwExV2(e);
		}
        // 正常发送流程开始
        map.put("tmpVAIList", tempVAIParam(map));
        
        // 手术暂时注释
        if (existsTmpVafs(map))
        {
            
            List tmpVATList = tempVATParam(map);
            for(int i=0;i<tmpVATList.size();i++)
            {
                Map<String, Object> temp999 = (Map<String, Object>)tmpVATList.get(i);
                temp999.put("cbm01", getNewId.nextId());
            }
            map.put("tmpVATList", tmpVATList);
            List tmpVAVList = tempVAVParam(map);
            map.put("tmpVAVList", tmpVAVList);
            
        }
        for (int i = 0; i < ((List<Map<String, Object>>)map.get("tmpVAJList")).size(); i++)
        {
            // ((Map<String,Object>)((List<Map<String,Object>>)map.get("tmpVAJList")).get(i)).put("bdn01", bdn01);
            ((Map<String, Object>)((List<Map<String, Object>>)map.get("tmpVAJList")).get(i)).put("vaj26", 0);
            ((Map<String, Object>)((List<Map<String, Object>>)map.get("tmpVAJList")).get(i)).put("vaj59",
            (((List<Map<String, Object>>)map.get("tmpVAJList")).get(i)).get("vaj33"));
            
        }
        map.put("tmpVAIList", tempVAIParam1(map));
        long l6 = 0;
        if (tmpVAJList != null && tmpVAJList.size() > 0)
        {
            // 费用单据
        	 FutureTask ftask_saveVAI_piplineMode=  saveVAI_piplineMode(map);
            long l4 = System.currentTimeMillis();
            log.info("----------------------------------------------保存单据耗时:"+((l4-l3))+"毫秒------------------------------------------------------");
            // 费用明细
           
           FutureTask ftask_saveVAJ_piplineMode=    saveVAJ_piplineMode(map);
            long l5 = System.currentTimeMillis();
            log.info("----------------------------------------------保存费用耗时:"+((l5-l4))+"毫秒------------------------------------------------------");
            // 修改单据
            ftask_saveVAI_piplineMode.get();ftask_saveVAJ_piplineMode.get();
            FutureTask ftask_updateVAI_piplineMode=  updateVAI_piplineMode(map);
            l6 = System.currentTimeMillis();
            log.info("----------------------------------------------更新单据字段耗时:"+((l6-l5))+"毫秒------------------------------------------------------");
        }
        //保存诊金
        List<Map<String,Object>> zjAddList = JsonUtil.getList(xmlJson.getString("zjAddList"));
        if(zjAddList!=null && !zjAddList.isEmpty())
        {
            List<Long> vaj01s = getNewId.nextId(zjAddList.size());
            long vai01 = getNewId.nextId();
            for(int i =0;i< zjAddList.size();i++)
            {
                Map<String,Object> vajMap = zjAddList.get(i);
                vajMap.put("vaa01", map.get("lVAA01"));
                vajMap.put("vaa07", map.get("lVAA07"));
                vajMap.put("bck01", map.get("lBCK01"));
                vajMap.put("bce02", map.get("lBCE02"));
                vajMap.put("bce03", map.get("lBCE03"));
                vajMap.put("abc02", map.get("abc02"));
                vajMap.put("vaj01", vaj01s.get(i));
            }
            Map<String,Object> zjMap = new HashMap<String,Object>();
            zjMap.put("vai01", vai01);
            zjMap.put("vaa01", map.get("lVAA01"));
            zjMap.put("vaa07", map.get("lVAA07"));
            zjMap.put("bck01", map.get("lBCK01"));
            zjMap.put("bce02", map.get("lBCE02"));
            zjMap.put("bce03", map.get("lBCE03"));
            zjMap.put("zjAddList", zjAddList);
            cliSendAdviceDao.saveZj(zjMap);
        }
        long l7 = System.currentTimeMillis();
        log.info("----------------------------------------------保存诊金费用耗时:"+((l7-l6))+"毫秒------------------------------------------------------");
        // 修改病人
     //   (map);
        long l8 = System.currentTimeMillis();
        log.info("----------------------------------------------更新医嘱信息耗时:"+((l8-l7))+"毫秒------------------------------------------------------");
        // 发送医嘱
        map.put("vbi01", getNewId.nextId());
        saveVBI(map);
        long l9 = System.currentTimeMillis();
        log.info("----------------------------------------------生成医嘱抄送记录耗时:"+((l9-l8))+"毫秒------------------------------------------------------");
        if (VBD1List != null && VBD1List.size() > 0)
        {
            // 医嘱计价
            saveVBD(map);
        }
        long l10 = System.currentTimeMillis();
        log.info("----------------------------------------------保存医嘱计价耗时:"+((l10-l9))+"毫秒------------------------------------------------------");
        // 病人医嘱状态
        saveVBG(map);
        if (VBGParam(map))
        {
            // 手术医嘱
            saveCMB1_VAT1_VAV1(map);
        }
        long l11 = System.currentTimeMillis();
        log.info("----------------------------------------------手术医嘱处理耗时:"+((l11-l10))+"毫秒------------------------------------------------------");
        // 医嘱执行通知   
        long l11a = System.currentTimeMillis();
        FutureTask  ftask_saveVBX1_pipline =  saveVBX1_pipline(map);
        FutureTask  ftask_changeStock_piplineMode =  changeStock_piplineMode(map);
        long l12 = System.currentTimeMillis();
        log.info("----------------------------------------------库存与处理耗时:"+((l12-l11a))+"毫秒------------------------------------------------------");
        long l12a = System.currentTimeMillis();
        FutureTask    ftask_stockLog_piplineMode =  stockLog_piplineMode(map);
        long l13 = System.currentTimeMillis();
        log.info("----------------------------------------------保存库存日志耗时:"+((l13-l12a))+"毫秒------------------------------------------------------");
        Map<String, Object> patientMap = new HashMap<String, Object>();
        patientMap.put("lVAC01", Long.parseLong(map.get("lVAA07").toString()));
        patientMap.put("productid", 100);
        patientMap.put("programid", 103010);
        patientMap.put("paramno", 29);
        List patientList = patientSearchBo.getPatientInfo(patientMap).getList();
        mo.setCommList(patientList);
        mo.setList((List)map.get("tmpVafsList"));
        mo.setRows((List)map.get("tmpVAJList"));
        //ati exec join
        ftask_saveVBX1_pipline.get();
        //ftask_changeStock_piplineMode.get();ftask_stockLog_piplineMode.get();
        long l14 = System.currentTimeMillis();
        log.info("===============================================发送医嘱结束耗时:"+((l14-l1))+"毫秒============================================-");
        return mo;
        }
        catch (Exception e)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            mo.setResultMsg(e.toString());
            e.printStackTrace();
            return mo;
        }
    }
    


	private void aa(Map map) {
		// TODO Auto-generated method stub
		
	}

	private void saveCMB1_VAT1_VAV1(Map map)
        throws Exception
    {
        cliSendAdviceDao.saveCMB1_VAT1_VAV1(map);
    }
    
    private void saveVBX1(Map map)
        throws Exception
    {
        cliSendAdviceDao.saveVBX1(map);
    }
    
    private boolean VBGParam(Map map)
        throws Exception
    {
        
        return cliSendAdviceDao.VBGParam(map) > 0;
    }
    
    private void saveVBD(Map map)
        throws Exception
    {
        cliSendAdviceDao.saveVBD(map);
    }
    
    private void saveVBG(Map map)
        throws Exception
    {
        cliSendAdviceDao.saveVBG(map);
    }
    
    private void saveVBI(Map map)
        throws Exception
    {
        cliSendAdviceDao.saveVBI(map);
    }
    
    private void saveVAI(Map map)
        throws Exception
    {
        cliSendAdviceDao.saveVAI(map);
    }
    
    private void saveVAJ(Map map)
        throws Exception
    {
        cliSendAdviceDao.saveVAJ(map);
    }
    
    private void updateVAI(Map map)
        throws Exception
    {
        cliSendAdviceDao.updateVAI(map);
    }
    
    private void updateVAF(Map map)
        throws Exception
    {
        cliSendAdviceDao.updateVAF(map);
    }
    
    private List tempVAIParam1(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tempVAIParam1(map);
    }
    
    private boolean existsTmpVafs(Map map)
        throws Exception
    {
        return cliSendAdviceDao.existsTmpVafs(map) > 0;
    }
    
    private List tempVAVParam(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tempVAVParam(map);
    }
    
    private List tempVATParam(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tempVATParam(map);
    }
    
    private List tempVAIParam(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tempVAIParam(map);
    }
    
    // 库存日志
    private void insertDeptlog(Map map)
        throws Exception
    {
        cliSendAdviceDao.insertDeptlog(map);
    }
    
    // 库存
    private List getDEPT(Map map)
        throws Exception
    {
        return cliSendAdviceDao.getDEPT(map);
    }
    
    // 材料库存
    private List getDEPTCl(Map map)
        throws Exception
    {
        return cliSendAdviceDao.getDEPTCl(map);
    }
    
    private Map getMinAndMaxVAI01(Map map)
        throws Exception
    {
        return cliSendAdviceDao.getMinAndMaxVAI01(map);
    }
    
    private boolean existsVAI1(Map map)
        throws Exception
    {
        return cliSendAdviceDao.existsVAI1(map) > 0;
    }
    
    private boolean checkStock(Map map)
        throws Exception
    {
        
        return cliSendAdviceDao.checkStock(map) > 0;
        
    }
    
    private String getBBY05(Map map)
        throws Exception
    {
        return cliSendAdviceDao.getBBY05(map);
    }
    
    private String existsBBY05(Map map)
        throws Exception
    {
        return cliSendAdviceDao.existsBBY05(map);
    }
    
    private List tempVafsParam(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tempVafsParam(map);
    }
    
    private List tempVAI1(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tempVAI1(map);
    }
    
    private List tempVaj1Param(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tempVaj1Param(map);
    }
    
    private Integer getTmpVaj1MaxID(Map map)
        throws Exception
    {
        return cliSendAdviceDao.getTmpVaj1MaxID(map);
    }
    
    private boolean Para258_2(Map map)
        throws Exception
    {
        return cliSendAdviceDao.Para258_2(map) > 0;
    }
    
    private Integer Para258_2_min(Map map)
        throws Exception
    {
        return cliSendAdviceDao.Para258_2_min(map);
    }
    
    private List Para258_2_tmpVAJ(Map map)
        throws Exception
    {
        return cliSendAdviceDao.Para258_2_tmpVAJ(map);
    }
    
    private Integer MinVAI01(Map map)
        throws Exception
    {
        return cliSendAdviceDao.MinVAI01(map);
    }
    
    private List para258tmpVAI(Map map)
        throws Exception
    {
        return cliSendAdviceDao.para258tmpVAI(map);
    }
    
    private List para258tmpVAJ(Map map)
        throws Exception
    {
        return cliSendAdviceDao.para258tmpVAJ(map);
    }
    
    private List getTmpVafs(Map map)
        throws Exception
    {
        return cliSendAdviceDao.getTmpVafs(map);
    }
    
    private List getTmpVBD(Map map)
        throws Exception
    {
        return cliSendAdviceDao.getTmpVBD(map);
    }
    
    // 收费方式
    private boolean VBD09(Map map)
        throws Exception
    {
        
        return cliSendAdviceDao.VBD09Count(map) > 0;
    }
    
    private List tempVBD1Param(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tempVBD1Param(map);
    }
    
    private boolean validity(Map map)
        throws Exception
    {
        
        return cliSendAdviceDao.validity(map) > 0;
    }
    
    private String expires(Map map)
        throws Exception
    {
        return cliSendAdviceDao.expires(map);
    }
    
    private Map getBCK01A(Map map)
        throws Exception
    {
        return cliSendAdviceDao.getBCK01A(map);
    }
    
    private boolean tmpZJExistsParam(Map map)
        throws Exception
    {
        Integer i = cliSendAdviceDao.getTmpZJ(map);
        Integer j = cliSendAdviceDao.VAJ1Count(map);
        if ((i > 0 && "1".equals(map.get("lACF01").toString())) && j == 0)
        {
            return true;
        }
        return false;
    }
    
    @SuppressWarnings("unused")
    private List TmpVAJParam(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tempVBD1Param(map);
    }
    
    private boolean cbm01Count(Map map)
        throws Exception
    {
        return cliSendAdviceDao.cbm01Count(map) > 0;
    }
    
    private List tmpVAIParam(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tmpVAIParam(map);
    }
    
    private List tmpVAJParam(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tmpVAJParam(map);
    }
    
    private List tmpVAIpara(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tmpVAIpara(map);
    }
    
    private Integer getMaxID(Map map)
        throws Exception
    {
        return cliSendAdviceDao.getMaxID(map);
    }
    
    private List setVAIID(Map map)
        throws Exception
    {
        return cliSendAdviceDao.setVAIID(map);
    }
    
    private List tmpVAJParam1(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tmpVAJParam1(map);
    }
    
    private List tmpVAIParam2(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tmpVAIParam2(map);
    }
    
    private List tmpVAJParam2(Map map)
        throws Exception
    {
        return cliSendAdviceDao.tmpVAJParam2(map);
    }
    
    public ModelVo getVbdInfo(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = cliSendAdviceDao.getVbdInfo(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
    }
    
    
	public FutureTask yaopinJinshouChk_pipline(final Map map, final ModelVo mo) throws Exception {
		 
		
		FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

  			@Override
  			public Object call() throws Exception   {				 
  			 
  					yaopinJinshouChk(map,mo);
				 
  			 
  				return "err";
  			}
  		}) ;
  		
  		 AsynUtil.execMeth_Ays( FutureTask_updateCviBdj,"");
		return FutureTask_updateCviBdj;
}

public void yaopinJinshouChk(Map map, ModelVo mo) throws Exception {
	String bby05 = existsBBY05(map);
	if (bby05 != null && !"".equals(bby05))
	{
	    mo.setResult(StaticKeys.OH_FALSE);
	    mo.setResultMsg(bby05 + "药品药房已经禁售");
	 //   return mo;
	    throw new RunEx("yaopinJinshouChk_err",mo);
	}
}

@SuppressWarnings("all")
public FutureTask validity_pipline(final Map map, final ModelVo mo) throws Exception {
	
	FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

			@Override
			public Object call()   {				 
				try {
				if (validity(map))
				{
				    String msg = expires(map);
				    mo.setResult(StaticKeys.OH_FALSE);
				    mo.setResultMsg(msg + "此药品有效期已过，不能继续使用");
				  throw new RunEx("validity_check_false",mo);
				  //  return mo;
				}
			} catch (Exception e) {
				ExUtil.throwExV2(e);
			}
				
//				if("a".equals("a"))
//				{
//					Map m=Maps.newConcurrentMap();m.put("k", "vv");
//			
//				 
//				}
				return "err";
			}
		}) ;
		
		 AsynUtil.execMeth_Ays( FutureTask_updateCviBdj,"");
	return FutureTask_updateCviBdj;
	
}
// 修改单据
private FutureTask updateVAI_piplineMode(final Map map) {
	FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

			@Override
			public Object call()   {				 
				try {
					 updateVAI(map);
				} catch (Exception e) {
					log.error(e);
				}
				return null;
			}
		}) ;
		
		 AsynUtil.execMeth_Ays( FutureTask_updateCviBdj,"");
	return FutureTask_updateCviBdj;
}

private FutureTask saveVAJ_piplineMode(final Map map) {
	FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

			@Override
			public Object call()   {				 
				try {
					 for(Map<String, Object> temp : (List<Map<String, Object>>)map.get("tmpVAJList"))
		            {
		                temp.put("vaj01", getNewId.nextId());
		            }
					 
					 
					saveVAJ(map);
				} catch (Exception e) {
					log.error(e);
				}
				return null;
			}
		}) ;
		
		 AsynUtil.execMeth_Ays( FutureTask_updateCviBdj,"");
	return FutureTask_updateCviBdj;
}

private FutureTask saveVAI_piplineMode(final Map map) {
	FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

			@Override
			public Object call()   {				 
				try {
					saveVAI(map);
				} catch (Exception e) {
					log.error(e);
				}
				return null;
			}
		}) ;
		
		 AsynUtil.execMeth_Ays( FutureTask_updateCviBdj,"");
	return FutureTask_updateCviBdj;
	
}

public FutureTask stockLog_piplineMode(final Map map) throws Exception {

	
	FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

			@Override
			public Object call()   {				 
				try {
					stockLog(map);
				} catch (Exception e) {
					log.error(e);
				}
				return null;
			}
		}) ;
		
		 AsynUtil.execMeth_Ays( FutureTask_updateCviBdj,"");
	return FutureTask_updateCviBdj;
}

public void stockLog(Map map) throws Exception {
	if (((List<Map<String,Object>>)map.get("tmpVafsList"))!=null && !((List<Map<String,Object>>)map.get("tmpVafsList")).isEmpty())
    {
        // 库存日志
        map.put("LBCE01", ((List<Map<String, Object>>)map.get("tmpVafsList")).get(0).get("bce01a"));
        map.put("LBCE03", ((List<Map<String, Object>>)map.get("tmpVafsList")).get(0).get("bce03a"));
    }
    insertDeptlog(map);
}

public FutureTask<Object> changeStock_piplineMode(final Map map) throws Exception {
	
	
	FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

			@Override
			public Object call()   {				 
				try {
					changeStock(map);
				} catch (Exception e) {
					log.error(e);
				}
				return null;
			}
		}) ;
		
		 AsynUtil.execMeth_Ays( FutureTask_updateCviBdj,"");
	return FutureTask_updateCviBdj;
	
	
	
}

protected void changeStock(Map map) throws Exception {
	List dpt = getDEPT(map);
    String Para46 = getParameter(9999, 9999, 46);
    if (dpt != null && dpt.size() > 0)
    {
      //预处理库存--根据参数来判断9999-9999-46
        if("0".equals(Para46))
        {
            for(int x = 0;x<dpt.size();x++)
            {
                yaopinKucunBo.gxDpsAndDpt(dpt.get(x));
            }
        }
    }
    // 材料库存预处理
    List dptCl = getDEPTCl(map);
    if (dptCl != null && dptCl.size() > 0)
    {
        //预处理库存--根据参数来判断9999-9999-46
        if("0".equals(Para46))
        {
            for (int i = 0; i < dptCl.size(); i++)
            {
                yaopinKucunBo.gxDpsAndDptCl(dptCl.get(i));
            }
        }
    }
	
}



private FutureTask saveVBX1_pipline(final Map map) {
	  FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

			@Override
			public Object call()   {				 
				try {
					saveVBX1(map);
				} catch (Exception e) {
					log.error(e);
				}
				return null;
			}
		}) ;
		
		 AsynUtil.execMeth_Ays( FutureTask_updateCviBdj,"");
	return FutureTask_updateCviBdj;
	
}
}
